package com.ruoyix.ai.util;

import cn.hutool.core.util.StrUtil;

import org.springframework.ai.chat.messages.*;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.openai.OpenAiChatOptions;


/**
 * Spring AI 工具类
 *
 * @author 芋道源码
 */
public class AiUtils {

    public static ChatOptions buildChatOptions(String platform, String model, Double temperature, Long maxTokens) {
        Float temperatureF = temperature != null ? temperature.floatValue() : null;
        //noinspection EnhancedSwitchMigration
        switch (platform) {
            case "OpenAI":
                return OpenAiChatOptions.builder().withModel(model).withTemperature(temperatureF).withMaxTokens(Math.toIntExact(maxTokens)).build();
            default:
                throw new IllegalArgumentException(StrUtil.format("未知平台({})", platform));
        }
    }

    public static Message buildMessage(String type, String content) {
        if (MessageType.USER.getValue().equals(type)) {
            return new UserMessage(content);
        }
        if (MessageType.ASSISTANT.getValue().equals(type)) {
            return new AssistantMessage(content);
        }
        if (MessageType.SYSTEM.getValue().equals(type)) {
            return new SystemMessage(content);
        }
        if (MessageType.FUNCTION.getValue().equals(type)) {
            return new FunctionMessage(content);
        }
        throw new IllegalArgumentException(StrUtil.format("未知消息类型({})", type));
    }

}
